package com.sgb.kjwl.view.ui.adapter.business;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.business.CompanyEntity;

import java.util.ArrayList;
import java.util.List;

public class CompanyPopupwindowAdapter extends RecyclerView.Adapter<CompanyPopupwindowAdapter.MyViewHolder> {
    //当前上下文对象
    Context context;
    //RecyclerView填充Item数据的List对象
    List<CompanyEntity> datas;
    /**
     * 是否添设置item选中状态
     */
    protected boolean isItemSelect = false;
    private int defItem = 0;

    public CompanyPopupwindowAdapter(Context context, List<CompanyEntity> datas){
        this.context = context;
        this.datas = datas;
    }

    public void setItemSelect(boolean itemSelect) {
        isItemSelect = itemSelect;
    }

    @Override
    public CompanyPopupwindowAdapter.MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        //实例化得到Item布局文件的View对象
        View v = View.inflate(context, R.layout.car_item_popwindows,null);
        //返回MyViewHolder的对象
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(CompanyPopupwindowAdapter.MyViewHolder myViewHolder, int position) {
        if (isItemSelect) {
            if (defItem == position) {
                myViewHolder.llItem.setBackgroundColor(context.getResources().getColor(R.color.color_F6F6F6));
            } else {
                myViewHolder.llItem.setBackgroundColor(context.getResources().getColor(R.color.colorWhite));
            }
        }

        myViewHolder.tvName.setText(datas.get(position).getName());

        if(mOnItemClickLitener != null){
            myViewHolder.llItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickLitener.onItemClick(myViewHolder.llItem, position);

                    notifyDataSetChanged();
                }
            });}
    }

    @Override
    public int getItemCount() {
        return datas==null?0:datas.size();
    }

    //继承RecyclerView.ViewHolder抽象类的自定义ViewHolder
    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView tvName;
        LinearLayout llItem;
        public MyViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            llItem = itemView.findViewById(R.id.ll_item);
        }
    }

    public void setDatas(List<CompanyEntity> datas) {
        this.datas = datas;
        if (this.datas == null) {
            this.datas = new ArrayList<>();
        }
        notifyDataSetChanged();
    }

    public interface OnItemClickLitener{
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener onItemClickLitener){
        this.mOnItemClickLitener = onItemClickLitener;
    }
}
